package com.free.mini.core.serve.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.mini.core.serve.entity.BannerConfig;
import com.free.mini.core.serve.mapper.BannerConfigMapper;
import com.free.mini.core.serve.service.IBannerConfigService;
import com.free.mini.core.serve.utils.IDGenerate;
import com.free.mini.core.server.vo.req.BannerConfigCreateReqVO;
import com.free.mini.core.server.vo.req.BannerConfigQueryReqVO;
import com.free.mini.core.server.vo.req.BannerConfigUpdateReqVO;
import com.free.mini.core.server.vo.resp.BannerConfigQueryRespVO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;

/**
 * banner Service实现
 *
 * @author dongsufeng
 * @date 2023-08-22 15:17:42
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class BannerConfigServiceImpl extends ServiceImpl<BannerConfigMapper, BannerConfig> implements IBannerConfigService {

    private final BannerConfigMapper bannerConfigMapper;

    @Override
    public Page<BannerConfigQueryRespVO> findBannerConfigs(BannerConfigQueryReqVO reqVO) {
        LambdaQueryWrapper<BannerConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BannerConfig::getDelFlag,0);
        queryWrapper.eq(reqVO.getLocation()!=null , BannerConfig::getLocation , reqVO.getLocation());
        Page<BannerConfig> page = this.page(new Page<>(reqVO.getPageIndex(),reqVO.getPageSize()), queryWrapper);
        List<BannerConfigQueryRespVO> respVOList = page.getRecords().stream().map(map ->
                BeanUtil.copyProperties(map, BannerConfigQueryRespVO.class)
        ).collect(Collectors.toList());
        Page<BannerConfigQueryRespVO> respVOPage = new Page<>();
        respVOPage.setRecords(respVOList);
        respVOPage.setCurrent(page.getCurrent());
        respVOPage.setTotal(page.getTotal());
        return respVOPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createBannerConfig(BannerConfigCreateReqVO reqVO) {
        BannerConfig bannerConfig = BeanUtil.copyProperties(reqVO, BannerConfig.class);
        bannerConfig.setId(IDGenerate.idGenerate());
        return this.save(bannerConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBannerConfig(BannerConfigUpdateReqVO reqVO) {
        BannerConfig bannerConfig = BeanUtil.copyProperties(reqVO, BannerConfig.class);
        return this.updateById(bannerConfig);
    }

    @Override
    public Boolean delete(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)){
            return false;
        }
        LambdaUpdateWrapper<BannerConfig> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(BannerConfig::getDelFlag,1);
        wrapper.in(BannerConfig::getId,ids);
        return this.update(wrapper);
    }

}
